#include "stm32f4xx.h"  
#include "uart4.h"     
#include "onenet_http.h"     
#include "wifi.h"     
#include "ali_mqtt.h"     

#if  USART4_RX_ENABLE                   
char Usart4_RxCompleted = 0;             
unsigned int Usart4_RxCounter = 0;      
char Usart4_RxBuff[USART4_RXBUFF_SIZE];    	
#endif

UART_HandleTypeDef UART4_Handler;
void Usart4_Init(unsigned int bound)
{ 
    UART4_Handler.Instance = UART4;
    UART4_Handler.Init.BaudRate = bound;
    UART4_Handler.Init.WordLength = UART_WORDLENGTH_8B;
    UART4_Handler.Init.StopBits = UART_STOPBITS_1;
    UART4_Handler.Init.Parity = UART_PARITY_NONE;
    UART4_Handler.Init.Mode = UART_MODE_TX_RX;
    HAL_UART_Init(&UART4_Handler);					    
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    __HAL_RCC_UART4_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**UART4 GPIO Configuration
      PA0-WKUP     ------> UART4_TX
      PA1     ------> UART4_RX
      */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(UART4_IRQn);

    UART4->CR1|=1<<3;  	
    UART4->CR1|=1<<2;  	
    UART4->CR1|=1<<5;    	
    UART4->CR1|=1<<13;  
}

char USART4_TxBuff[USART4_TXBUFF_SIZE];  
//__align(8) char USART4_TxBuff[USART4_TXBUFF_SIZE];  

void u4_printf(char* fmt,...) 
{  
    unsigned int i,length;

    va_list ap;
    va_start(ap,fmt);
    vsprintf(USART4_TxBuff,fmt,ap);
    va_end(ap);	

    length=strlen((const char*)USART4_TxBuff);		
    while((UART4->SR&0X40)==0);
    for(i = 0;i < length;i ++)
    {			
        UART4->DR = USART4_TxBuff[i];
        while((UART4->SR&0X40)==0);	
    }	
}

void UART4_IRQHandler(void)   
{                      
    if((__HAL_UART_GET_FLAG(&UART4_Handler,UART_FLAG_RXNE)!=RESET)) { 
		if(Connect_flag==0){                                //如果Connect_flag等于0，当前还没有连接服务器，处于指令配置状态
			if(UART4->DR){                                 //处于指令配置状态时，非零值才保存到缓冲区	
				Usart4_RxBuff[Usart4_RxCounter]=UART4->DR; //保存到缓冲区	
				Usart4_RxCounter ++;                        //每接收1个字节的数据，Usart2_RxCounter加1，表示接收的数据总量+1 
			}		
		}else{		                                        //反之Connect_flag等于1，连接上服务器了	
			Usart4_RxBuff[Usart4_RxCounter] = UART4->DR;   //把接收到的数据保存到Usart2_RxBuff中				
			if(Usart4_RxCounter == 0){    					//如果Usart2_RxCounter等于0，表示是接收的第1个数据，进入if分支				
				//TIM_Cmd(TIM4,ENABLE);
                Usart4_RxCompleted = 0;
                
			}else{                        					//else分支，表示果Usart2_RxCounter不等于0，不是接收的第一个数据
				//TIM_SetCounter(TIM4,0);  
			}	
			Usart4_RxCounter ++;         				    //每接收1个字节的数据，Usart2_RxCounter加1，表示接收的数据总量+1 
		}		
    }
    else if (__HAL_UART_GET_FLAG(&UART4_Handler, UART_FLAG_IDLE)) {
        if (Connect_flag == 1) {
            u3_printf("Usart4_RxBuff:%s\r\n", (char *)Usart4_RxBuff);                               
            u3_printf("Usart4_RxCounter:%d\r\n", (char *)Usart4_RxCounter);                               
            memcpy(&MQTT_RxDataInPtr[2],Usart4_RxBuff,Usart4_RxCounter);  //拷贝数据到接收缓冲区
            MQTT_RxDataInPtr[0] = Usart4_RxCounter/256;                   //记录数据长度高字节
            MQTT_RxDataInPtr[1] = Usart4_RxCounter%256;                   //记录数据长度低字节
            MQTT_RxDataInPtr+=RBUFF_UNIT;                                  //指针下移
            if(MQTT_RxDataInPtr==MQTT_RxDataEndPtr)                       //如果指针到缓冲区尾部了
                MQTT_RxDataInPtr = MQTT_RxDataBuf[0];                     //指针归位到缓冲区开头
            WiFi_RxCounter=0;                                             
            Usart4_RxCompleted = 1;                                      
        }

        __HAL_UART_CLEAR_IDLEFLAG(&UART4_Handler);//must clear
    }
} 

/*-------------------------------------------------*/
/*函数名：串口4发送缓冲区中的数据                  */
/*参  数：data：数据                               */
/*返回值：无                                       */
/*-------------------------------------------------*/
void u4_TxData(unsigned char *data)
{
    int	i;	
    while((UART4->SR&0X40)==0);
    for(i = 1;i <= (data[0]*256+data[1]);i ++){			
        UART4->DR = data[i+1];
        while((UART4->SR&0X40)==0);	
    }
}
